package com.sip.comadapter.executor;

import java.util.HashSet;
import java.util.Set;

import com.sip.comadapter.common.BaseExecutor;
import com.sip.comadapter.executor.common.Executor;
import com.sip.comserver.communication.CommunicationMessage;
import com.sip.dbadapter.dao.domain.ApiDefinition;
import com.sip.dbadapter.dao.domain.ConsumeTemplate;
import com.sip.dbadapter.dao.domain.ScanTypeRest;
import com.sip.dbadapter.dao.domain.ScanTypeRmi;
import com.sip.dbadapter.dao.domain.ScanTypeWs;
import com.sip.dbadapter.dao.exception.CustomException;
import com.sip.dbadapter.dao.exception.CustomExceptionConstant;
import com.sip.dbadapter.dao.interfaces.ApiDao;
import com.sip.dbadapter.dao.interfaces.ConsumerTemplateSaveDao;
import com.sip.dbadapter.dao.utility.DaoLoadFactory;
import com.sip.dbadapter.dao.utility.DaoNameConstant;

public class TemplateSaveExecutor extends BaseExecutor implements Executor {





    public CommunicationMessage execute(CommunicationMessage message) throws Exception {

        System.out.println("<<<<<<<<<<<<<<< TemplateSaveExecutor >>>>>>>>>>>>>>>");
        final CommunicationMessage communicationMessage = new CommunicationMessage();

        ScanTypeWs scanTypeWs = null;
        ScanTypeRest scanTypeRest = null;
        ScanTypeRmi scanTypeRmi = null;
        Set<ConsumeTemplate> consumeTemplateSet = new HashSet<ConsumeTemplate>();
        
        ConsumerTemplateSaveDao templateSaveDao = loadConsumerTemplateSaveDao();
        ApiDao dao=loadApiDefinitionDao();
        // ConsumeTemplate consumeTemplate = new ConsumeTemplate();

        // Retrieve input data from CommunicationMessage
        ApiDefinition apiDef = (ApiDefinition) message.getObject();
        Long apiId = apiDef.getApiId();
        consumeTemplateSet = apiDef.getScanTypeRest().getConsumeTemplateSet();
        System.out.println("==========API id==========================>" + apiId);
        
        //check consumer Template Id is available
        ConsumeTemplate consumerTemplateObj=null;
        CustomException customException=null;
        for (ConsumeTemplate consumeTemplate : consumeTemplateSet) {
          String templateId=  consumeTemplate.getTemplateId();
        
          consumerTemplateObj=templateSaveDao.findById(templateId);
          if(null!=consumerTemplateObj){
           customException= new CustomException("Template Id already Exists", CustomExceptionConstant.TEMPLATE_ID_EXIST_EXCEPTION);
          break;
          }
        }


       
       
      
        // get ApiDefinition for selected ApiId.
        ApiDefinition apiDefinition = dao.findById(apiId);

        scanTypeWs = apiDefinition.getScanTypeWs();
        scanTypeRest = apiDefinition.getScanTypeRest();
        scanTypeRmi = apiDefinition.getScanTypeRmi();
        
   

        if (null != scanTypeRest) {// TODO- null check should be changed after implementing RMI and WS ScanType
                                   // Strategies. ScanType should check with the Type value which comes with
                                   // CommuncationMessage's ApiDefinition Object.eg. if ScanType=1==> Rest /if ScanType==>2 RMI

            setRestData(scanTypeRest, consumeTemplateSet, apiDefinition);

        } else if (null != scanTypeRmi) {
            setRmiData(scanTypeRmi, consumeTemplateSet, apiDefinition);
        } else if (null != scanTypeWs) {

            setWSData(scanTypeWs, consumeTemplateSet, apiDefinition);
            


        }
     
        
         //Exception is set if Template Id is exists
        if(null==customException){
        dao.update(apiDefinition);
        communicationMessage.setObject(apiDefinition);
         }else{
         communicationMessage.setObject(customException);  
         }
        
       
//
       

       
        return communicationMessage;
    }
/**
 * 
 * @param scanTypeWs
 * @param consumeTemplateSet
 * @param apiDefinition
 */
    private void setWSData(ScanTypeWs scanTypeWs, Set<ConsumeTemplate> consumeTemplateSet, ApiDefinition apiDefinition) {

        for (ConsumeTemplate consumeTemplate : consumeTemplateSet) {
           
            consumeTemplate.setType(3);// 3 -WS
            consumeTemplate.setScanTypeWs(scanTypeWs);
            consumeTemplateSet.add(consumeTemplate);
        }

        scanTypeWs.setConsumeTemplateSet(consumeTemplateSet);
        apiDefinition.setScanTypeWs(scanTypeWs);
     
    }
/**
 * 
 * @param scanTypeRmi
 * @param consumeTemplateSet
 * @param apiDefinition
 */
    private void setRmiData(ScanTypeRmi scanTypeRmi, Set<ConsumeTemplate> consumeTemplateSet, ApiDefinition apiDefinition) {

        for (ConsumeTemplate consumeTemplate : consumeTemplateSet) {
            consumeTemplate.setScanTypeRmi(scanTypeRmi);
            consumeTemplate.setType(2);// 2 - RMI
            consumeTemplateSet.add(consumeTemplate);
        }

        scanTypeRmi.setConsumeTemplateSet(consumeTemplateSet);
        apiDefinition.setScanTypeRmi(scanTypeRmi);
       
    }
/**
 * 
 * @param scanTypeRest
 * @param consumeTemplateSet
 * @param apiDefinition
 */
    private void setRestData(ScanTypeRest scanTypeRest, Set<ConsumeTemplate> consumeTemplateSet, ApiDefinition apiDefinition) {

        for (ConsumeTemplate consumeTemplate : consumeTemplateSet) {
            consumeTemplate.setScanTypeRest(scanTypeRest);
            consumeTemplate.setType(1);// 1 -REST
            consumeTemplateSet.add(consumeTemplate);
            
        }

        scanTypeRest.setConsumeTemplateSet(consumeTemplateSet);
        apiDefinition.setScanTypeRest(scanTypeRest);
       
    }
/**
 * 
 * @return
 */
    private ConsumerTemplateSaveDao loadConsumerTemplateSaveDao() throws Exception {

        ConsumerTemplateSaveDao templateSaveDao = null;

 
            templateSaveDao = (ConsumerTemplateSaveDao) DaoLoadFactory.getDao(DaoNameConstant.TEMPLATE_SAVE_DAO);

        return templateSaveDao;
    }
    
    private ApiDao loadApiDefinitionDao() throws Exception {

        ApiDao apiDef=(ApiDao)DaoLoadFactory.getDao(DaoNameConstant.API_DAO);
        return apiDef;
        
        
     
    }
}
